﻿using System;

namespace OpenWaves.ImageTransformations
{
    public class ScaleToFillTransformation : ImageTransformation
    {
        public ScaleToFillTransformation(int width, int height) : base(width, height)
        {
        }

        public ScaleToFillTransformation(string serializedProperties) : base(serializedProperties)
        {
        }

        public override void ApplyToImage(IImage image)
        {
            int width;
            int height;
            if ((double)image.Width / image.Height < (double)this.Width / this.Height)
            {
                width = this.Width;
                height = (int)(Math.Round((double)this.Width / image.Width * image.Height));
            }
            else
            {
                width = (int)(Math.Round((double)this.Height / image.Height * image.Width));
                height = this.Height;
            }

            image.Scale(width, height);

            if (width > this.Width || height > this.Height)
                image.Crop((int) ((width-this.Width)/2.0), (int) ((height-this.Height)/2.0), this.Width, this.Height);
        }

        protected override IImageTransformation Scale(int width, int height)
        {
            return new ScaleToFillTransformation(width, height);
        }
    }
}